package com.bwie.record.task;

import com.alibaba.fastjson.JSON;
import com.bwie.record.domain.DataOne;
import com.bwie.record.domain.DataThree;
import com.bwie.record.domain.DataTwo;
import com.bwie.record.generator.NameGenerator;
import com.bwie.record.generator.RandomIDGenerator;
import com.bwie.record.generator.RandomLicensePlateGenerator;
import com.bwie.record.generator.RandomNumberGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName SpringTask
 * @Description 描述
 * @Author ZiZheng.Li
 * @Date 2024/7/7 10:04
 */
@Slf4j
@Component
@EnableScheduling
public class ScheduledTask {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    private ScheduledExecutorService executorService = Executors.newScheduledThreadPool(3);

    DataOne dataOne = new DataOne();
    DataTwo dataTwo = new DataTwo();
    DataThree dataThree = new DataThree();
    @Scheduled(cron = "0 0/3 * * * ?") // 每三分钟执行一次
    public void generateDataAndSendToQueue() {
        Random random = new Random();


        String id = RandomNumberGenerator.generateRandomNumber();
        String xm= NameGenerator.generateThreeCharacterName();
        String idCard = RandomIDGenerator.generateRandomID();
        String plate = RandomLicensePlateGenerator.generateRandomLicensePlate();

        dataOne.setId(id);
        dataOne.setXm(xm);
        dataOne.setCard(idCard);
        dataOne.setAge(random.nextInt(100));
        dataOne.setTime(new Date());

        dataTwo.setId(id);
        dataTwo.setXm(xm);
        dataTwo.setCard(idCard);
        dataTwo.setPhotoOne("111");
        dataTwo.setPhotoTwo("222");

        dataThree.setId(id);
        dataThree.setPlate(plate);
        dataThree.setTime(new Date());

        // 将数据发送到对应的消息队列
        rabbitTemplate.convertAndSend("dataOneQueue", JSON.toJSONString(dataOne));
        rabbitTemplate.convertAndSend("dataTwoQueue", JSON.toJSONString(dataTwo));
        rabbitTemplate.convertAndSend("dataThreeQueue", JSON.toJSONString(dataThree));
        init();
    }

    public void init() {
        executorService.scheduleAtFixedRate(this::sendDataOne, 0, 1, TimeUnit.MINUTES);
        executorService.scheduleAtFixedRate(this::sendDataTwo, 2, 1, TimeUnit.MINUTES);
        executorService.scheduleAtFixedRate(this::sendDataThree, 4, 1, TimeUnit.MINUTES);
    }

    private void sendDataOne() {
        log.info("data1发送时间："+new Date().toLocaleString());
        rabbitTemplate.convertAndSend("dataOneQueue", JSON.toJSONString(dataOne));
    }

    private void sendDataTwo() {
        log.info("data2发送时间："+new Date().toLocaleString());
        rabbitTemplate.convertAndSend("dataTwoQueue", JSON.toJSONString(dataTwo));
    }

    private void sendDataThree() {
        log.info("data3发送时间："+new Date().toLocaleString());
        rabbitTemplate.convertAndSend("dataThreeQueue", JSON.toJSONString(dataThree));
    }
}
